package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.AccountUserLastPosDao;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserLastPos;
import com.tbit.main.pojo.GeoreGionBound;
import com.tbit.main.pojo.TerPosition;
import com.tbit.main.service.AccountUserLastPosService;
import com.tbit.main.service.AccountUserService;
import com.tbit.main.service.GeoreGionService;
import com.tbit.main.service.RoleService;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2021-03-25 17:40
 * 运维人员最后位置
 */
@Service("accountUserLastPosService")
public class AccountUserLastPosServiceImpl implements AccountUserLastPosService {

    @Autowired
    private AccountUserLastPosDao accountUserLastPosDao;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private RoleService roleService;

    @Override
    public void insert(AccountUserLastPos accountUserLastPos) {
        accountUserLastPosDao.insert(accountUserLastPos);
    }

    @Override
    public void update(TerPosition terPositions) {
        accountUserLastPosDao.update(terPositions);
    }

    /*@Override
    public void updateBatch(){
        accountUserLastPosDao.updateBatch(SystemData.userHistory.get(SysConstant.HISTORY).stream().filter(distinctByKey(l ->l.getMachineId())).collect(Collectors.toList()));
    }*/

    public <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    @Override
    public AccountUserLastPos getByAccountUserId(Integer accountUserId) {
        return accountUserLastPosDao.getByAccountUserId(accountUserId);
    }

    @Override
    public List<AccountUserLastPos> getByAccountId(Integer agentId, Integer userType, Integer usermainId, List<Integer> regionId) {

        /**查询代理商运维账号*/
        List<Integer> accountUserIds = new ArrayList<Integer>();
        /**判断当前账号是不是运维人员*/
        if (roleService.getRoleCode(userType).equals(1)) {
            accountUserIds.add(usermainId);
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, agentId);
        params.put("userMainIds", StringUtil.getListString(accountUserIds));
        if (CollUtil.isNotEmpty(regionId)) {
            List<GeoreGionBound> georeGionBounds = georeGionService.getBoundRegionIds(regionId);
            accountUserIds = georeGionBounds.stream().map(GeoreGionBound::getAccountUserId).collect(Collectors.toList());

            accountUserIds = accountUserIds.stream().filter(e -> e != null).collect(Collectors.toList());
            if (accountUserIds == null || accountUserIds.size() == 0) {
                return new LinkedList<>();
            }
            params.put("userMainIds", StringUtil.getListString(accountUserIds));
        }

        List<AccountUserLastPos> accountUserLastPos = accountUserLastPosDao.getByAccountId(params);

        if (accountUserLastPos.size() > 0) {
            for (AccountUserLastPos dls : accountUserLastPos) {
                AccountUser au = accountUserService.getById(dls.getAccountUserId());
                if (au != null) {
                    dls.setUserName(au.getName());
                }
            }
        }
        return accountUserLastPos;
    }

    @Override
    public List<AccountUserLastPos> getAll() {
        return accountUserLastPosDao.getAll();
    }
}
